﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietPremier.Areas.AdminCP.Models;
using MvcLibrary.Repository;
using System.IO;

namespace VietPremier.Areas.AdminCP.Controllers
{
    public class AdminUserManagerController : Controller
    {
        #region "Index"
        public ActionResult Index()
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                AdminUserManagerModel model = new AdminUserManagerModel();

                foreach (var adminuser in db.EntityAdmins.OrderBy(o => o.Type))
                {
                    model.listAdminUserManager.Add(new AdminUserManager()
                    {
                        ID = adminuser.Id,
                        username = adminuser.Username,
                        type = adminuser.Type
                    });
                }
                return View(model);
            }
        }
        #endregion //index

        #region "Delete"
        public string Delete(int id)
        {
            try
            {
                dbVietPremierDataContext db = new dbVietPremierDataContext();
                EntityAdmin item = db.EntityAdmins.Where(p => p.Id == id).Single();
                db.EntityAdmins.DeleteOnSubmit(item);
                db.SubmitChanges();
                return "Ok";
            }
            catch
            {
                return "Error";

            }

        }
        #endregion

        #region "Insert"
        public ActionResult Insert()
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                AdminUserManager model = new AdminUserManager();

                 return PartialView(model);
            }
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Insert(AdminUserManager model)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                EntityAdmin adminuser = new EntityAdmin();
                adminuser.Username = model.username;
                adminuser.Type = model.type;

                Security ser = new Security();
                string hex = ser.GetHashPassword(model.password);
                adminuser.Password = hex;

                db.EntityAdmins.InsertOnSubmit(adminuser);
                db.SubmitChanges();

                return RedirectToAction("Index", "AdminUserManager");
            }
        }
        #endregion

        #region "Edit"
        public ActionResult Edit(int idUserManager)
        {
            return PartialView();
        }

        [HttpPost]
        public JsonResult GetDetail(int idUserManager)
        {
            AdminUserManager model = new AdminUserManager();
            model.ID = -1;

            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                EntityAdmin adminuser = db.EntityAdmins.FirstOrDefault(o => o.Id == idUserManager);

                if (adminuser != null)
                {
                    model.ID = adminuser.Id;
                    model.username = adminuser.Username;
                    model.type = adminuser.Type;

                }
                return Json(model);
            }

        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(int Id, string UserName, string Type, string Password)
        {

            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                //check Id 
                EntityAdmin adminuser = db.EntityAdmins.Where(p => p.Id == Id).FirstOrDefault();
                adminuser.Username = UserName;
                adminuser.Type = Type;
                adminuser.Password = Password;

                db.SubmitChanges();
                return RedirectToAction("Index", "AdminUserManager");
            }
        }

        #endregion //edit

    }
}
